package com.sansan.qiangji.realize.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.warehouse.WarehouseListResultVO;
import com.sansan.qiangji.VO.base.warehouse.WarehouseVO;
import com.sansan.qiangji.entity.base.Warehouse;
import com.sansan.qiangji.entity.base.example.WarehouseExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.WarehouseAddForm;
import com.sansan.qiangji.form.base.WarehouseEditForm;
import com.sansan.qiangji.form.base.WarehouseListForm;
import com.sansan.qiangji.service.inter.base.WarehouseService;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WarehouseReal {
    @Autowired
    private WarehouseService warehouseService;

    @Transactional
    public void deleteWarehouse(Integer WarehouseId){
        Warehouse Warehouse = warehouseService.selectByPrimaryKey(WarehouseId);
        if(Warehouse==null){
            log.error("【仓库删除】仓库不存在，WarehouseId={}",WarehouseId);
            throw new ServiceException(ResultEnum.WAREHOUSE_IS_NOT_EXIST);
        }
        int row = warehouseService.deleteByPrimaryKey(WarehouseId);
        if(row==0){
            log.error("【仓库删除】仓库删除失败，WarehouseId={}",WarehouseId);
            throw new ServiceException(ResultEnum.WAREHOUSE_DELETE_ERROR);
        }
    }


    @Transactional
    public void addWarehouse(WarehouseAddForm warehouseAddForm){
        WarehouseExample example = new WarehouseExample();
        example.or().andWarehouseNameEqualTo(warehouseAddForm.getWarehouseName());
        List<Warehouse> warehouseList = warehouseService.selectByExample(example);
        if(warehouseList.size()>0){
            log.error("【仓库新增】该仓库名称或编号已存在，warehouseName={}",warehouseAddForm.getWarehouseName());
            throw new ServiceException(ResultEnum.WAREHOUSE_IS_EXIST);
        }
        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(warehouseAddForm,warehouse);
        int row = warehouseService.insert(warehouse);
        if(row==0){
            log.error("【仓库新增】仓库新增失败");
            throw new ServiceException(ResultEnum.WAREHOUSE_INSERT_ERROR);
        }
    }


    @Transactional
    public void editWarehouse(WarehouseEditForm warehouseEditForm){
        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(warehouseEditForm,warehouse);
        int row = warehouseService.updateByPrimaryKey(warehouse);
        if(row==0){
            log.error("【仓库新增】仓库新增失败");
            throw new ServiceException(ResultEnum.WAREHOUSE_INSERT_ERROR);
        }
    }

    public WarehouseListResultVO warehouseList(WarehouseListForm warehouseListForm){
        WarehouseExample example = new WarehouseExample();
        int pageNum = warehouseListForm.getPageNum();
        int pageSize = warehouseListForm.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        WarehouseExample.Criteria criteria = example.createCriteria();
        if(warehouseListForm.getWarehouseName()!=null&&!"".equals(warehouseListForm.getWarehouseName())){
            criteria.andWarehouseNameLike("%"+warehouseListForm.getWarehouseName()+"%");
        }
        List<Warehouse> warehouseList = warehouseService.selectByExample(example);
        PageResult<Warehouse> warehousePageResult = PageUtil.getPageResult(new PageInfo<>(warehouseList));
        WarehouseListResultVO warehouseListResultVO = new WarehouseListResultVO();
        BeanUtils.copyProperties(warehousePageResult,warehouseListResultVO);
        List<WarehouseVO> warehouseVOS = new ArrayList<>();
        for(Warehouse warehouse : warehousePageResult.getContent()){
            WarehouseVO warehouseVO = new WarehouseVO();
            BeanUtils.copyProperties(warehouse,warehouseVO);
            warehouseVOS.add(warehouseVO);
        }
        warehouseListResultVO.setWarehouseVOS(warehouseVOS);
        return warehouseListResultVO;
    }

    public List<WarehouseVO> getAllWarehouse(){
        WarehouseExample example = new WarehouseExample();
        List<Warehouse> warehouseList = warehouseService.selectByExample(example);
        List<WarehouseVO> warehouseVOS = new ArrayList<>();
        for (Warehouse warehouse : warehouseList){
            WarehouseVO warehouseVO = new WarehouseVO();
            BeanUtils.copyProperties(warehouse, warehouseVO);
            warehouseVOS.add(warehouseVO);
        }
        return warehouseVOS;
    }
}
